package com.swind.fast.modules.app.halali.customer.controller;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.swind.fast.common.api.vo.Result;
import com.swind.fast.common.system.query.QueryGenerator;
import com.swind.fast.common.util.oConvertUtils;
import com.swind.fast.common.constant.CommonConstant;
import com.swind.fast.modules.app.halali.customer.entity.HalaliUser;
import com.swind.fast.modules.app.halali.customer.entity.Restaurant;
import com.swind.fast.modules.app.halali.customer.service.IRestaurantService;
import com.swind.fast.modules.app.halali.customer.entity.RestaurantCategory;
import com.swind.fast.modules.app.halali.customer.service.IRestaurantCategoryService;
import com.swind.fast.modules.app.halali.customer.entity.RestaurantMeal;
import com.swind.fast.modules.app.halali.customer.service.IRestaurantMealService;
import com.swind.fast.modules.app.halali.customer.entity.RestaurantMealCategory;
import com.swind.fast.modules.app.halali.customer.service.IRestaurantMealCategoryService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.swind.fast.common.system.base.controller.SwindController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
// import com.swind.fast.common.aspect.annotation.AutoLog;

import com.swind.fast.modules.system.entity.SysAnnouncement;
import com.swind.fast.modules.system.entity.SysAnnouncementSend;
import com.swind.fast.modules.system.service.ISysAnnouncementSendService;
import com.swind.fast.modules.system.service.ISysAnnouncementService;

 /**
 * @Description: 餐厅
 * @Author: swind
 * @Date:   2022-01-20
 * @Version: V1.0
 */
// @Api(tags="餐厅管理")
@RestController
@RequestMapping("/app/customer/restaurant")
@Slf4j
public class RestaurantController extends SwindController<Restaurant, IRestaurantService> {
	@Autowired
	private IRestaurantService restaurantService;

	@Autowired
	private IRestaurantCategoryService restaurantCategoryService;

	@Autowired
	private IRestaurantMealService restaurantMealService;

	@Autowired
	private IRestaurantMealCategoryService restaurantMealCategoryService;

	// @Autowired
	// private ISysAnnouncementService sysAnnouncementService;

	/**
	 * 分页列表查询
	 *
	 * @param restaurant
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	// @AutoLog(value = "合同-分页列表查询")
	// @ApiOperation(value="餐厅-分页列表查询", notes="餐厅-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Restaurant restaurant,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Restaurant> queryWrapper = QueryGenerator.initQueryWrapper(restaurant, req.getParameterMap());
		Page<Restaurant> page = new Page<Restaurant>(pageNo, pageSize);
		IPage<Restaurant> pageList = restaurantService.page(page, queryWrapper);

		pageList.getRecords().stream().forEach(item -> {
            item.setDeliveryTime("30");
            item.setDeliveryDistance("2.5");
        });

		return Result.OK(pageList);
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	// @ApiOperation(value="餐厅详情-通过id查询", notes="账单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id", required=true) String id) {
		Restaurant restaurant = restaurantService.getById(id);
		if (restaurant == null) {
			return Result.error("未找到对应数据");
		} else {
			// 获取餐厅分类
			QueryWrapper<RestaurantCategory> queryWrapper = new QueryWrapper<RestaurantCategory>().eq("restaurant_id", restaurant.getId());
			List<RestaurantCategory> list = restaurantCategoryService.list(queryWrapper);
			// log.info("category: {}", list);

			// 获取餐厅餐品
			QueryWrapper<RestaurantMeal> tqueryWrapper = new QueryWrapper<RestaurantMeal>().eq("restaurant_id", restaurant.getId());
			List<RestaurantMeal> tlist = restaurantMealService.list(tqueryWrapper);

			if (oConvertUtils.isNotEmpty(tlist)) {
				List<String> tlistIds = tlist.stream().map(RestaurantMeal::getId).collect(Collectors.toList());
				// log.info("category: {}", tlist);

				// 获取餐厅餐品分类
				QueryWrapper<RestaurantMealCategory> pqueryWrapper = new QueryWrapper<RestaurantMealCategory>().in("meal_id", tlistIds);
				List<RestaurantMealCategory> plist = restaurantMealCategoryService.list(pqueryWrapper);

				for (RestaurantCategory category : list) {
					// 获取该类别下所有餐品
					List<RestaurantMealCategory> citems = plist.stream().filter(e -> e.getRestaurantCategoryId().equals(category.getId())).collect(Collectors.toList());
					// 获取该类别下所有餐品IDs
					List<String> mealIds = citems.stream().map(RestaurantMealCategory::getMealId).distinct().collect(Collectors.toList());
					// 获取该类别下所有餐品详情
					List<RestaurantMeal> flist = tlist.stream().filter(e -> (mealIds.contains(e.getId()))).collect(Collectors.toList());

					// log.info("meal category: {}", citems);
					// log.info("meal mealIds: {}", mealIds);
					// log.info("meal plist: {}", flist);
					category.setMeals(flist);
				}
			}

			restaurant.setCategories(list);
		}
		return Result.OK(restaurant);
	}
}
